package cn.edu.hncst.service.impl;

import cn.edu.hncst.dao.UserDao;
import cn.edu.hncst.dao.impl.UserDaoImpl;
import cn.edu.hncst.entity.User;
import cn.edu.hncst.service.UserService;
import java.util.List;

public class UserServiceImpl implements UserService {
    private UserDao userDao = new UserDaoImpl();

    @Override
    public List<User> queryAll() {
        return userDao.queryAll();
    }

    @Override
    public int addUser(User user) {
        return userDao.addObject(user);
    }

    @Override
    public int updateUser(User user) {
        return userDao.updateObject(user,user.getId());
    }

    @Override
    public int deleteById(Integer id) {
        return userDao.deleteById(id);
    }

    @Override
    public int[] deleteByIds(String... ids) {
        return userDao.deleteByIds(ids);
    }

    @Override
    public List<User> pageQuery(int startCount, int pageSize) {
        return userDao.pageQuery(startCount,pageSize);
    }

    @Override
    public User findById(String username, String password) {
        return userDao.findById(username,password);
    }

    @Override
    public int queryTotalCount() {
        List<User> count = userDao.queryAll();
        return count.size();
    }

    @Override
    public List<User> searchUser(String keywords, int startCount, int pageSize) {
        List<User> users = userDao.searchUser(keywords, startCount, pageSize);
        for (User user : users) {
            if(user.getName().contains(keywords)) {
                user.setName(user.getName().replace(keywords,"<span style='color:#FF7744'>"+keywords+"</span>"));
            }
            if(user.getGender().contains(keywords)) {
                user.setGender(user.getGender().replace(keywords,"<span style='color:#FF7744'>"+keywords+"</span>"));
            }
            if(user.getAddress().contains(keywords)) {
                user.setAddress(user.getAddress().replace(keywords,"<span style='color:#FF7744'>"+keywords+"</span>"));
            }
            if(user.getQq().contains(keywords)) {
                user.setQq(user.getQq().replace(keywords,"<span style='color:#FF7744'>"+keywords+"</span>"));
            }
            if(user.getEmail().contains(keywords)) {
                user.setEmail(user.getEmail().replace(keywords,"<span style='color:#FF7744'>"+keywords+"</span>"));
            }
        }
        return users;
    }

    @Override
    public int querySearchCount(String keywords) {
        return userDao.querySearchCount(keywords);
    }


    public static void main(String[] args) {
        UserService userDao = new UserServiceImpl();
        List<User> users = userDao.searchUser("c",0,10);
        System.out.println(users);
    }
}
